﻿@page "/MyBaoGong"
@using System.Diagnostics.CodeAnalysis
@using Microsoft.Extensions.Localization
@inject SqlSugarScope _SqlSugarScope
@inject AuthenticationStateProvider authenticationStateProvider
@rendermode InteractiveServer
@layout MainLayout2


<BootstrapBlazorRoot>
<h1> @username</h1>

<DemoBlock Title="@username"
           Name="IsStriped">
        <div class="row g-3">
            <div class="col-3 col-sm-auto">
                <DateTimePicker @bind-Value="@startdate" />
            </div>
         @*    <div class="col-3 col-sm-auto">
                <DateTimePicker @bind-Value="@enddate" />
            </div>
            <div class="col-3 col-sm-auto"> <Select IsVirtualize="true" Items="VirtualItems" @bind-Value="VirtualItem2"></Select> </div>
            <Button Icon="fa-solid fa-arrows-rotate" Text="@RefreshText" OnClick="OnClick" class="col-3 col-sm-auto" /> *@
        </div>
        <div class="row g-3">
            <div class="col-3 col-sm-auto">
                <DateTimePicker @bind-Value="@enddate" />
            </div>
         @*    <div class="col-3 col-sm-auto"> <Select IsVirtualize="true" Items="VirtualItems" @bind-Value="VirtualItem2"></Select> </div>
            <Button Icon="fa-solid fa-arrows-rotate" Text="@RefreshText" OnClick="OnClick" class="col-3 col-sm-auto" /> *@
        </div>
        <div class="row g-3">
            <div class="col-3 col-sm-auto"> <Select IsVirtualize="true" Items="VirtualItems" @bind-Value="VirtualItem2"></Select> </div>
            <Button Icon="fa-solid fa-arrows-rotate" Text="@RefreshText" OnClick="OnClick" class="col-3 col-sm-auto" />
        </div>


        <Table TItem="Bus_合同分拆报工" RenderMode="TableRenderMode.Table" Items="@Items" IsStriped="true" >
        <TableColumns>
                <TableColumn @bind-Field="@context.施工开始时间" />
                <TableColumn @bind-Field="@context.报工班组" />
                <TableColumn @bind-Field="@context.总包合同项目名称" />
                <TableColumn @bind-Field="@context.道路信息" />
                <TableColumn @bind-Field="@context.时间跨度" />
                <TableColumn @bind-Field="@context.任务名称" />
                <TableColumn @bind-Field="@context.结算类型" />
                <TableColumn @bind-Field="@context.单位" />
                <TableColumn @bind-Field="@context.管径" />
                <TableColumn @bind-Field="@context.经理作业量" />
                <TableColumn @bind-Field="@context.作业量" />
                <TableColumn @bind-Field="@context.定额名称" />
                <TableColumn @bind-Field="@context.人数" />
                <TableColumn @bind-Field="@context.施工人员" />
                <TableColumn @bind-Field="@context.备注" />
        </TableColumns>
    </Table>

</DemoBlock>
</BootstrapBlazorRoot>
@code {
    private string username;
    private IEnumerable<SelectedItem> VirtualItems ;
    private SelectedItem? VirtualItem2 { get; set; }

    [NotNull]
    private string? RefreshText { get; set; }
    [NotNull]
    private List<Bus_合同分拆报工>? Items { get; set; }

    private DateTime? startdate { get; set; }
    private DateTime? enddate { get; set; }


    protected override async Task OnInitializedAsync()
    {
        var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        username = user.Identity.Name;
        RefreshText ??= "刷新";
        // //刷新界面
        // StateHasChanged();
        VirtualItems = 人员数据权限();
    }


    private void OnClick()
    {
        查询();
        StateHasChanged();
    }

    private List<SelectedItem> 人员数据权限()
    { 
        //可以查看的人员
        Dic_人员数据权限 dic_人员数据权限 = _SqlSugarScope.Queryable<Dic_人员数据权限>()
                                                    .Where(t => t.人员编号 == username).First();
        List<SelectedItem> 乙方项目经理s = new List<SelectedItem>();
        乙方项目经理s.Add(new SelectedItem(username, username));
        if (dic_人员数据权限 != null && string.IsNullOrEmpty(dic_人员数据权限.人员数据) == false)
        {
            List<string> s = dic_人员数据权限?.人员数据.Split(',').ToList();

            if (s != null && s.Count > 0)
            {
                for (int i = 0; i < s.Count; i++)
                {
                    SelectedItem selectedItem = new SelectedItem();
                    selectedItem.Text = s[i];
                    selectedItem.Value = s[i];
                    乙方项目经理s.Add(selectedItem);
                }
            }           
        }
        return 乙方项目经理s;
    }

    private void 查询()
    {
        

        Items = _SqlSugarScope.Queryable<Bus_合同分拆报工>().Where(it => it.Creator == VirtualItem2.Text)
                                                                  .WhereIF(startdate != null, it => it.施工开始时间 >= startdate)
                                                                  .WhereIF(enddate != null, it => it.施工开始时间 < enddate.Value.AddDays(1))
                                                                  .OrderBy(it => new { it.Creator,it.施工开始时间 })
                                                                 .ToList();
    }
}
